package com.ycy.head;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.WriteTable;
import lombok.SneakyThrows;

import java.util.*;

public class ExcelHeaderDemo {

    @SneakyThrows
    public static void main(String[] args) {
        List<List<String>> heads = new ArrayList<>();
        // 示例数据
        String json = "{\"children\":[{\"code\":\"1735182337587\",\"title\":\"新单元格\",\"name\":\"field1\",\"dataType\":\"STR\",\"width\":150,\"sortCriteria\":false,\"mustFill\":false,\"children\":[{\"code\":\"1735182345251\",\"title\":\"新单元格\",\"name\":\"field1-1\",\"dataType\":\"STR\",\"width\":150,\"sortCriteria\":false,\"mustFill\":false,\"children\":[{\"code\":\"1735183731787\",\"title\":\"新单元格\",\"name\":\"field1-1-1\",\"dataType\":\"STR\",\"width\":150,\"sortCriteria\":false,\"mustFill\":false,\"children\":[{\"code\":\"1735183865115\",\"title\":\"新单元格\",\"name\":\"field1-1-1-1\",\"dataType\":\"STR\",\"width\":150,\"sortCriteria\":false,\"mustFill\":false,\"children\":[{\"code\":\"1735190198837\",\"title\":\"新单元格1\",\"name\":\"field1-1-1-1-1\",\"dataType\":\"STR\",\"width\":150,\"sortCriteria\":false,\"mustFill\":false,\"children\":[]}]}]},{\"code\":\"1735191224369\",\"title\":\"新单元格2\",\"name\":\"field1-1-2\",\"dataType\":\"STR\",\"width\":150,\"sortCriteria\":false,\"mustFill\":false,\"children\":[]}]}]},{\"code\":\"1735182341632\",\"title\":\"新单元格\",\"name\":\"field2\",\"dataType\":\"STR\",\"width\":150,\"sortCriteria\":false,\"mustFill\":false,\"children\":[{\"code\":\"1735190193837\",\"title\":\"新单元格\",\"name\":\"field2-1\",\"dataType\":\"STR\",\"width\":150,\"sortCriteria\":false,\"mustFill\":false,\"children\":[{\"code\":\"1735190196653\",\"title\":\"新单元格3\",\"name\":\"field2-1-1\",\"dataType\":\"STR\",\"width\":150,\"sortCriteria\":false,\"mustFill\":false,\"children\":[]},{\"code\":\"1735191481783\",\"title\":\"新单元格4\",\"name\":\"field2-1-2\",\"dataType\":\"STR\",\"width\":150,\"sortCriteria\":false,\"mustFill\":false,\"children\":[]},{\"code\":\"1735191219826\",\"title\":\"新单元格5\",\"name\":\"field2-1-3\",\"dataType\":\"STR\",\"width\":150,\"sortCriteria\":false,\"mustFill\":false,\"children\":[]}]}]}]}";
        json = "{\"children\":[{\"code\":\"1735182337587\",\"title\":\"新单元格\",\"name\":\"field1\",\"dataType\":\"STR\",\"width\":150,\"sortCriteria\":false,\"mustFill\":false,\"children\":[{\"code\":\"1735182345251\",\"title\":\"新单元格\",\"name\":\"field1-1\",\"dataType\":\"STR\",\"width\":150,\"sortCriteria\":false,\"mustFill\":false,\"children\":[{\"code\":\"1735183731787\",\"title\":\"新单元格\",\"name\":\"field1-1-1\",\"dataType\":\"STR\",\"width\":150,\"sortCriteria\":false,\"mustFill\":false,\"children\":[{\"code\":\"1735183865115\",\"title\":\"新单元格\",\"name\":\"field1-1-1-1\",\"dataType\":\"STR\",\"width\":150,\"sortCriteria\":false,\"mustFill\":false,\"children\":[{\"code\":\"1735190198837\",\"title\":\"新单元格1\",\"name\":\"field1-1-1-1-1\",\"dataType\":\"STR\",\"width\":150,\"sortCriteria\":false,\"mustFill\":false,\"children\":[]}]}]},{\"code\":\"1735191224369\",\"title\":\"新单元格2\",\"name\":\"field1-1-2\",\"dataType\":\"STR\",\"width\":150,\"sortCriteria\":false,\"mustFill\":false,\"children\":[]}]}]},{\"code\":\"1735182341632\",\"title\":\"新单元格\",\"name\":\"field2\",\"dataType\":\"STR\",\"width\":150,\"sortCriteria\":false,\"mustFill\":false,\"children\":[{\"code\":\"1735190193837\",\"title\":\"新单元格\",\"name\":\"field2-1\",\"dataType\":\"STR\",\"width\":150,\"sortCriteria\":false,\"mustFill\":false,\"children\":[{\"code\":\"1735190196653\",\"title\":\"新单元格3\",\"name\":\"field2-1-1\",\"dataType\":\"STR\",\"width\":150,\"sortCriteria\":false,\"mustFill\":false,\"children\":[]},{\"code\":\"1735191481783\",\"title\":\"新单元格4\",\"name\":\"field2-1-2\",\"dataType\":\"STR\",\"width\":150,\"sortCriteria\":false,\"mustFill\":false,\"children\":[]},{\"code\":\"1735191219826\",\"title\":\"新单元格5\",\"name\":\"field2-1-3\",\"dataType\":\"STR\",\"width\":150,\"sortCriteria\":false,\"mustFill\":false,\"children\":[]}]}]}]}"
        ;
        // 解析 JSON 数据
        HeadLineContentNodeVo rootNode = HeadLineContentNodeVo.parseConentNode(json);
        List<List<HeadLineContentNodeVo>> headList = rootNode.createHeadList();

        int sheet = 0;
        String space = "\t";
        List<List<String>> head = new ArrayList<>();
        List<String> headCode = new ArrayList<>();
        // code 不重复, 但是组件的中文名称可能重复,中文名称重复可能会出现 错误的合并单元格,所以通过给中文名称添加 空白指标符的方式区分合并单元格
        Map<String,List<String>> headDupCodeMap = new HashMap<>();
        for (List<HeadLineContentNodeVo> headLineContentNodeVos : headList) {
            //取每列的表头
            HeadLineContentNodeVo headLineContentNodeVo = headLineContentNodeVos.get(headLineContentNodeVos.size() - 1);
            headCode.add(headLineContentNodeVo.getCode());
            List<String> headPath = new ArrayList<>();
            head.add(headPath);
            for (int i = 0; i < headLineContentNodeVos.size(); i++) {

            }
            for (HeadLineContentNodeVo lineContentNodeVo : headLineContentNodeVos) {
                String title = lineContentNodeVo.getTitle();
                String code = lineContentNodeVo.getCode();
                headDupCodeMap.putIfAbsent(title, new ArrayList<>());
                List<String> strings = headDupCodeMap.get(title);
                int indexOf = strings.indexOf(code);
                int newInx = 0;
                if(indexOf < 0){
                    strings.add(code);
                    newInx = strings.size()-1;
                }else{
                    // 查询当前出现的位置
                    newInx = indexOf;
                }
                String spaceSuffux = "";
                for (int j = 0; j < newInx; j++) {
                    spaceSuffux += space;
                }
                headPath.add(title + spaceSuffux);
            }
        }

        // 文件输出路径
        String fileName = "E:\\linshi\\test1" + System.currentTimeMillis() + ".xlsx";

        // 创建数据
        List<List<String>> data = new ArrayList<>();
        data.add(Arrays.asList("张三", "男", "20", "北京", "本科"));
        data.add(Arrays.asList("李四", "女", "22", "上海", "硕士"));

        for (List<String> strings : head) {
            for (String string : strings) {
                System.out.print(string);
            }
            System.out.println();
        }

        // 写入 Excel
        EasyExcel.write(fileName)
                .head(head)
                .sheet(0,"Sheet1")
                .doWrite(data);

        // 写入 Excel
        EasyExcel.write(fileName)
                .head(head)
                .sheet(1,"Sheet2")
                .doWrite(data);

    }


    public static void main2(String[] args) {
        // 文件输出路径
        String fileName = "E:\\linshi\\test1" + System.currentTimeMillis() + ".xlsx";

        // 创建数据
        List<List<String>> data = new ArrayList<>();
        data.add(Arrays.asList("张三", "男", "20", "北京", "本科"));
        data.add(Arrays.asList("李四", "女", "22", "上海", "硕士"));

        // 创建多级表头
        List<List<String>> head = new ArrayList<>();

        List<String> objects0 = new ArrayList<>();
        objects0.addAll(Arrays.asList("do\t", "基本信息", "姓名"));
        head.add(objects0);

        List<String> objects1 = new ArrayList<>();
        objects1.addAll(Arrays.asList("do\t", "基本信息", "性别"));
        head.add(objects1);

        List<String> objects2 = new ArrayList<>();
        objects2.addAll(Arrays.asList("do", "基本信息", "年龄"));
        head.add(objects2);

        List<String> objects3 = new ArrayList<>();
        objects3.addAll(Arrays.asList( "详细信息", "地址"));
        head.add(objects3);

        List<String> objects4 = new ArrayList<>();
        objects4.addAll(Arrays.asList( "详细信息", "学历"));
        head.add(objects4);

        // 写入 Excel
        EasyExcel.write(fileName)
                .head(head)
                .sheet("Sheet1")
                .doWrite(data);
    }

}
